package cn.aishua.data;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class ResourceDataParser
{

    public LinkedHashMap<Item, Map<Integer, Item>> category;
    public LinkedHashMap<Item, Map<Integer, Item>> canton;

    TreeMap<Integer, Item> temp;
    JSONObject obj;

    public ResourceDataParser(String raw)
    {
        category = new LinkedHashMap<Item, Map<Integer, Item>>();
        canton = new LinkedHashMap<Item, Map<Integer, Item>>();

        temp = new TreeMap<Integer, Item>();
        parseCategory( raw );
        parseCanton( raw );
    }

    private void parseCategory( String raw )
    {
        try
        {
            obj = new JSONObject( raw );
            JSONArray pcat = obj.getJSONArray( "parent_categories" );
            for( int i = 0; i < pcat.length(); i++ )
            {
                temp.put( pcat.getJSONObject( i ).getInt( "id" ), makeItem( pcat.getJSONObject( i ) ) );
            }

            // add item for all categories of vendor - start
            TreeMap<Integer, Item> allCates = new TreeMap<Integer, Item>();
            allCates.put( 0, new Item( 0, "全部商户", 0 ) );
            category.put( new Item( 0, "全部商户" ), allCates );
            // add item for all categories of vendor - end

            for( Entry<Integer, Item> e : temp.entrySet() )
            {
                TreeMap<Integer, Item> map = new TreeMap<Integer, Item>();
                Item parentCat = e.getValue();
                // add sub_category item which is for all
                map.put( 0, new Item( 0, "全部" + parentCat.name, parentCat.id ) );
                category.put( parentCat, map );
            }

            JSONArray scat = obj.getJSONArray( "sub_categories" );
            for( int i = 0; i < scat.length(); i++ )
            {
                Item item = makeItem( scat.getJSONObject( i ) );

                for( Entry<Item, Map<Integer, Item>> e : category.entrySet() )
                {
                    if( e.getKey().id == item.pid )
                    {
                        e.getValue().put( item.id, item );
                        break;
                    }
                }
            }
        }
        catch( JSONException e )
        {
            e.printStackTrace();
        }

    }

    private void parseCanton( String raw )
    {
        try
        {
            if( obj == null )
            {
                obj = new JSONObject( raw );
            }
            JSONArray c = obj.getJSONArray( "cantons" );
            for( int i = 0; i < c.length(); i++ )
            {
                temp.put( c.getJSONObject( i ).getInt( "id" ), new Item( c.getJSONObject( i ).getInt( "id" ), c
                    .getJSONObject( i ).getString( "district_name" ), 0 ) );
            }

            // add canton item of range - start
            LinkedHashMap<Integer, Item> value = new LinkedHashMap<Integer, Item>();
            value.put( 500, new Item( 500, "附近500米", 0 ) );
            value.put( 1000, new Item( 1000, "附近1000米", 0 ) );
            value.put( 2000, new Item( 2000, "附近2000米", 0 ) );
            value.put( 5000, new Item( 0, "全城" ) );
            canton.put( new Item( 0, "范围" ), value );
            // add canton item of range - end

            for( Entry<Integer, Item> e : temp.entrySet() )
            {

                TreeMap<Integer, Item> map = new TreeMap<Integer, Item>();
                Item cant = e.getValue();
                // add business_circles item which is for all
                map.put( e.getValue().id, new Item( 0, "全部" + cant.name, cant.id ) );
                canton.put( cant, map );

                //canton.put(e.getValue(), new TreeMap<Integer, Item>());
            }

            for( int i = 0; i < c.length(); i++ )
            {
                JSONObject o = c.getJSONObject( i );
                for( Entry<Item, Map<Integer, Item>> e : canton.entrySet() )
                {
                    if( e.getKey().id == o.getInt( "id" ) )
                    {
                        JSONArray bc = o.getJSONArray( "business_circles" );

                        for( int j = 0; j < bc.length(); j++ )
                        {
                            e.getValue().put( bc.getJSONObject( j ).getInt( "id" ), makeItem( bc.getJSONObject( j ) ) );
                        }
                        break;
                    }
                }
            }

        }
        catch( JSONException e )
        {
            e.printStackTrace();
        }

    }

    private Item makeItem( JSONObject o ) throws JSONException
    {
        return new Item( o.getInt( "id" ), o.getString( "name" ), o.optInt( "pid" ) );
    }

    public void print()
    {
        for( Entry<Item, Map<Integer, Item>> e : category.entrySet() )
        {
            Log.e( "test", "P item id " + e.getKey().id + ", name " + e.getKey().name );
            for( Entry<Integer, Item> e1 : e.getValue().entrySet() )
            {
                Log.d( "test", "---S item id " + e1.getKey() + ", name " + e1.getValue().name );
            }
        }

        for( Entry<Item, Map<Integer, Item>> e : canton.entrySet() )
        {
            Log.e( "test", "D item id " + e.getKey().id + ", name " + e.getKey().name );
            for( Entry<Integer, Item> e1 : e.getValue().entrySet() )
            {
                Log.d( "test", "---BC item id " + e1.getKey() + ", name " + e1.getValue().name );
            }
        }

    }

    public class Item
    {
        public int id;
        public String name;
        public int pid;

        public Item(int i, String n)
        {
            id = i;
            name = n;
            pid = 0;
        }

        public Item(int i, String n, int p)
        {
            id = i;
            name = n;
            pid = p;
        }
    }

}
